package com.topteam.graph;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.mikephil.charting.charts.CombinedChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * Date:Create by 2021/6/22
 * Describe: 柏拉图
 * author: leo
 * Email: le112233@126.com
 */
public class Fragment_Plato extends Fragment{

    private View view;
    private CombinedChart plato_v;//图表
    private CombinedData data;  //柏拉图数据

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.plato_fragment,container,false);
        plato_v = view.findViewById(R.id.plato);
        showPlato();
        return view;
    }

    /**
     * 展现柏拉图
     */
    private void showPlato(){
        //设置图表描述
        Legend legend = plato_v.getLegend();
        legend.setTextColor(Color.WHITE);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);

        //绘制图表数据
        data = new CombinedData();
        //设置折线图数据
        data.setData(getLineData());
        //设置柱状图数据
        data.setData(getBarData());
        plato_v.setData(data);
        //设置横坐标数据
        setAxisXBottom();
        //设置右侧纵坐标数据
        setAxisYRight();
        //设置左侧纵坐标数据
        setAxisYLeft();
        plato_v.setTouchEnabled(false);
        plato_v.getDescription().setEnabled(false);
        plato_v.setDrawGridBackground(false);
        plato_v.setDrawBarShadow(false);
        plato_v.setHighlightFullBarEnabled(false);
        plato_v.animateX(2000);

    }

    /**
     * 设置横坐标数据
     */
    private void setAxisXBottom() {
        List<String> valuesX = new ArrayList<>();
        valuesX.add("1月");
        valuesX.add("2月");
        valuesX.add("3月");
        valuesX.add("4月");
        valuesX.add("5月");
        valuesX.add("6月");
        valuesX.add("7月");
        valuesX.add("8月");
        valuesX.add("9月");
        valuesX.add("10月");
        valuesX.add("11月");
        valuesX.add("12月");

        //X轴的数据格式
        XAxis xAxis = plato_v.getXAxis();
        xAxis.setValueFormatter(new IndexAxisValueFormatter(valuesX));
        //设置位置
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        //设置是否绘制网格线
        xAxis.setDrawGridLines(false);
        plato_v.getAxisLeft().setDrawGridLines(false);
        // barChart.animateY(2500);
        //设置X轴文字剧中对齐
        xAxis.setCenterAxisLabels(false);
        //X轴最小间距
        xAxis.setGranularity(1f);
        //设置柱子的总数量
        xAxis.setLabelCount(10);
        //设置字体大小
        xAxis.setTextSize(10);
        //设置第一条柱子距离左边的距离
        xAxis.setSpaceMin(0.4f);
        xAxis.setSpaceMax(0.4f);
        xAxis.setTextColor(Color.WHITE);

    }

    /**
     * 设置右侧纵坐标数据
     */
    private void setAxisYRight() {
        YAxis right = plato_v.getAxisRight();
        right.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return value + "%";
            }
        });
        right.setDrawGridLines(false);
        right.setAxisMinimum(0f);//为坐标轴设置最小值
        right.setAxisMaximum(100);
        right.setTextSize(15);
        right.setTextColor(Color.RED);
    }

    /**
     * 设置左侧纵坐标数据
     */
    private void setAxisYLeft() {
        YAxis left = plato_v.getAxisLeft();
        left.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                int aa = (int)value;
                return aa + "";
            }
        });
        //是否绘制网格线
        left.setDrawGridLines(false);
        left.setAxisMinimum(0f);//为坐标轴设置最小值
        left.setAxisMaximum(2000);

        left.setDrawZeroLine(false);
        left.setTextSize(15);
        left.setTextColor(Color.GREEN);
    }

    /**
     * 设置折线图绘制数据
     * @return
     */
    public LineData getLineData() {
        LineData lineData = new LineData();
        List<Entry> customCounts = new ArrayList<>();
        customCounts.add(new Entry(0,23));
        customCounts.add(new Entry(1,45));
        customCounts.add(new Entry(2,63));
        customCounts.add(new Entry(3,79));
        customCounts.add(new Entry(4,50));
        customCounts.add(new Entry(5,10));
        customCounts.add(new Entry(6,35));
        customCounts.add(new Entry(7,55));
        customCounts.add(new Entry(8,70));
        customCounts.add(new Entry(9,88));
        customCounts.add(new Entry(10,95));
        customCounts.add(new Entry(11,70));

        //设置描述
        LineDataSet lineDataSet = new LineDataSet(customCounts, "品质不良");
        //设置折线图依据哪边取值
        lineDataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
        //设置折线的颜色
        lineDataSet.setColor(Color.parseColor("#FF0000"));
        lineDataSet.setValueFormatter(new IValueFormatter(){

            @Override
            public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                return value+"";
            }
        });
        lineDataSet.setCircleRadius(5);
        // lineDataSet.setLineWidth(3);
        //设置字体大小
        lineDataSet.setValueTextSize(14);
        //设置字体颜色
        lineDataSet.setValueTextColor(Color.parseColor("#FF0000"));
        lineData.addDataSet(lineDataSet);
        return lineData;
    }

    /**
     * 设置柱状图绘制数据
     *
     * @return
     */
    public BarData getBarData() {
        BarData barData = new BarData();
        ArrayList<BarEntry> yVals1 = new ArrayList<>();
        yVals1.add(new BarEntry(0, 599));
        yVals1.add(new BarEntry(1, 899));
        yVals1.add(new BarEntry(2, 900));
        yVals1.add(new BarEntry(3, 1477));
        yVals1.add(new BarEntry(4, 1000));
        yVals1.add(new BarEntry(5, 500));
        yVals1.add(new BarEntry(6, 1699));
        yVals1.add(new BarEntry(7, 1899));
        yVals1.add(new BarEntry(8, 1320));
        yVals1.add(new BarEntry(9, 700));
        yVals1.add(new BarEntry(10, 500));
        yVals1.add(new BarEntry(11, 1432));

        BarDataSet amountBar = new BarDataSet(yVals1, "生产数量");
        //设置柱状图的颜色
        amountBar.setColor(Color.GREEN);
        //设置柱形图上面的字体大小
        amountBar.setValueTextSize(14);
        //设置描述方块的大小
        // amountBar.setFormSize(15);
        amountBar.setValueTextColor(Color.GREEN);

        barData.addDataSet(amountBar);

        //设置柱状图显示的大小
        float barWidth = 0.7f;
        barData.setBarWidth(barWidth);
        return barData;
    }

}
